package org.casscommunity.sights.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class MessageDialog {

	interface MessageDialogBinder extends UiBinder<DialogBox, MessageDialog> {}
	private static MessageDialogBinder uiBinder = GWT.create(MessageDialogBinder.class);
	
	@UiField
	DialogBox dialogBox;
	
	@UiField
	HTML htmlBox;
	
	@UiField
	SimplePanel widgetPanel;
	
	@UiField(provided = true)
	Label message;
	
	public MessageDialog() {
		this("", "");
	}
	
	public MessageDialog(String titleText, String messageText) {
		message = new Label(messageText);
		uiBinder.createAndBindUi(this);
		dialogBox.setText(titleText);
		message.setVisible(true);
	}
	
	@UiHandler("okButton")
	void onClickOk(ClickEvent event) {
		dialogBox.hide();
	}
	
	public void show() {
		dialogBox.center();
	}
	
	public void show(String titleText, String messageText) {
		setTitleText(titleText);
		setMessage(messageText);
		show();
	}
	
	public void showHtmlMessage(String titleText, String html) {
		setTitleText(titleText);
		setHtmlContent(html);
		show();
	}
	
	public void show(String title, Widget widget) {
		setTitleText(title);
		setWidgetPanel(widget);
		show();
	}
	
	public void setMessage(String messageText) {
		message.setText(messageText);
		toggleLabelVisible();
	}
	
	public void setTitleText(String titleText) {
		dialogBox.setText(titleText);
	}
	
	public void setHtmlContent(String html) {
		htmlBox.setHTML(html);
		toggleHtmlVisible();
	}
	
	public void setWidgetPanel(Widget widget) {
		widgetPanel.setWidget(widget);
		toggleWidgetVisible();
	}
	
	private void toggleLabelVisible() {
		message.setVisible(true);
		htmlBox.setVisible(false);
		widgetPanel.setVisible(false);
	}
	
	private void toggleHtmlVisible() {
		message.setVisible(false);
		htmlBox.setVisible(true);
		widgetPanel.setVisible(false);
	}
	
	private void toggleWidgetVisible() {
		message.setVisible(false);
		htmlBox.setVisible(false);
		widgetPanel.setVisible(true);
	}

}
